import api from '@/api';

import { ADDRESS_TYPE, TRANSPORT_TYPE } from '@/Constants';

export default {
  namespace: 'address',

  state: {
    consignee: {
      expressAddress: [],
      shippingAddress: [],
      airAddress: [],
    },
    shipper: {
      expressAddress: [],
      shippingAddress: [],
      airAddress: [],
    },
    pickUp: {
      expressAddress: [],
      shippingAddress: [],
      airAddress: [],
    },
    delivery: {
      expressAddress: [],
      shippingAddress: [],
      airAddress: [],
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const params = {
        queryParams: {
          isNewArea: true,
          ...payload,
        },
      };
      let res = yield call(api.account.address.query, params);
      yield put({
        type: 'saveAddress',
        payload: {
          ...payload,
          data: res.data,
          pagination: res.pagination
        },
      });
    },
    *add({ payload }, { call, put }) {
      const params = {
        queryParams: {
          isNewArea: true,
        },
        data: {
          ...payload,
        },
      };
      return yield call(api.account.address.create, params);
    },
    *setDefault({ payload }, { call, put }) {
      const params = {
        queryParams: {
          ...payload,
        },
      };
      yield call(api.account.address.setDefault, params);
    },
    *update({ payload }, { call, put }) {
      const params = {
        queryParams: {
          isNewArea: true,
        },
        pathParams: {
          id: payload.id,
        },
        data: {
          ...payload,
        },
      };
      yield call(api.account.address.update, params);
    },
    *delete({ payload }, { call, put }) {
      const params = {
        data: payload,
      };
      return yield call(api.account.address.remove, params);
    },
    *fetchServiceDatas(_, { call }) {
      const [lines, points] = yield [
        call(api.address.serviceLine),
        call(api.address.servicePoint)
      ];
      return {
        lines,
        points
      }
    }
  },

  reducers: {
    saveAddress(state, action) {
      let { type, addressType, data, pagination } = action.payload;
      if (type == ADDRESS_TYPE.CONSIGNEE) {
        let newConsignee = { ...state.consignee };
        switch (addressType) {
          case TRANSPORT_TYPE.EXPRESS:
            newConsignee.expressAddress = {data, pagination};
            break;
          case TRANSPORT_TYPE.AIR:
            newConsignee.airAddress = {data, pagination};
            break;
        }
        return { ...state, consignee: newConsignee };
      }

      if (type == ADDRESS_TYPE.SHIPPER) {
        let newShipper = { ...state.shipper };
        switch (addressType) {
          case TRANSPORT_TYPE.EXPRESS:
            newShipper.expressAddress = {data, pagination};
            break;
          case TRANSPORT_TYPE.AIR:
            newShipper.airAddress = {data, pagination};
            break;
        }
        return { ...state, shipper: newShipper };
      }

      if (type == ADDRESS_TYPE.PICK_UP) {
        let newPickUp = { ...state.pickUp };
        switch (addressType) {
          case TRANSPORT_TYPE.EXPRESS:
            newPickUp.expressAddress = {data, pagination};
            break;
          case TRANSPORT_TYPE.AIR:
            newPickUp.airAddress = {data, pagination};
            break;
        }
        return { ...state, pickUp: newPickUp };
      }

      if (type == ADDRESS_TYPE.DELICIOUS) {
        let newDelivery = { ...state.delivery };
        switch (addressType) {
          case TRANSPORT_TYPE.EXPRESS:
            newDelivery.expressAddress = {data, pagination};
            break;
          case TRANSPORT_TYPE.AIR:
            newDelivery.airAddress = {data, pagination};
            break;
        }
        return { ...state, delivery: newDelivery };
      }

      return state;
    },
  },
};
